new Vue({
  el: '#app',
  data() {
    return {
      form: {
        serachKey: "",
        page: 1,			//页数
        limit: 10			//每页数据条数
      },
      formData: {},
      article_info: [],
      store_info: [],
      supplier_info: [],
      tableData: [{}],		//表格数据
      total: 1,
      dialogForm: false,
      dialogVisible: false,
      fileList: [],
      loading: false,
      title: '',
      multipleSelection: [],
      form2: []
    }
  },
  mounted() {
    let _this = this
    $.ajax({
      type: 'POST',
      url: DomainName + '/GoodsFileInquiry',
      data: {
        Data: 'article_info',
        token: $.cookie("token")
      },
      success: function (data) {
        _this.article_info = data
        console.log(data);
      },
      dataType: "json"
    });
    $.ajax({
      type: 'POST',
      url: DomainName + '/GoodsFileInquiry',
      data: {
        Data: 'store_info'
      },
      success: function (data) {
        _this.store_info = data
      },
      complete: function (XMLHttpRequest) {
        _this.loading = false
        if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
          window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
        }
      },
      dataType: "json"
    });
    $.ajax({
      type: 'POST',
      url: DomainName + '/GoodsFileInquiry',
      data: {
        Data: 'supplier_info'
      },
      success: function (data) {
        _this.supplier_info = data
      },
      complete: function (XMLHttpRequest) {
        _this.loading = false
        if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
          window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
        }
      },
      dataType: "json"
    });
    this.getList1()
  },
  computed: {
    formatTime() {
      return function (time) {
        if (time) {
          let d = new Date(parseInt(time));
          return `${d.getFullYear()}-${d.getMonth() + 1
            }-${d.getDate()} ${d.getHours()}:${d.getMinutes()}:${d.getSeconds()}`;
        } else {
          return ''
        }
      };
    },
    Article_Name() {
      return function (id) {
        if (this.article_info.length > 0) {
          let obj = this.article_info.filter(item => {
            return item.Article_SerialNumber == id
          })[0]
          return obj.Article_Name
        } else {
          return ''
        }

      }
    },
    Store_Name() {
      return function (id) {
        if (this.store_info.length > 0) {
          let obj = this.store_info.filter(item => {
            return item.Store_SerialNumber == id
          })[0]
          return obj.Store_Name
        } else {
          return ''
        }
      }
    },
    Supplier_Name() {
      return function (id) {
        if (this.supplier_info.length > 0) {
          let obj = this.supplier_info.filter(item => {
            return item.Supplier_SerialNumber == id
          })[0]
          return obj.Supplier_Name
        } else {
          return ''
        }
      }
    },
  },
  methods: {
    onSieve() {				//查询方法
      if (Sieve) {
        this.tableData = this.tableData.filter(item => item.Row_Status == '1')
        this.Sieve = !this.Sieve
      } else {
        this.getList1()
        this.Sieve = !this.Sieve
      }
    },
    addEnterprise() {
      let _this = this
      this.loading = true
      $.ajax({
        type: 'POST',
        url: DomainName + '/OrderInfo_Add',
        data: {
          Data: JSON.stringify({
            order_info: _this.form2
          })
        },
        success: function (data) {
          if (data.Status == "success") {
            _this.$message.success("提交成功")
            _this.dialogVisible = false
            _this.getList1()
          }
        },
        complete: function (XMLHttpRequest) {
          _this.loading = false
          if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
            window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
          }
        },
        dataType: "json"
      });
    },
    handleSizeChange(val) {
      this.form.page = 1;
      this.form.limit = val;
    },
    handleCurrentChange(val) {
      this.form.page = val;
    },
    getList1() {
      let _this = this
      $.ajax({
        type: 'POST',
        data: { token: $.cookie("token") },
        url: DomainName + '/OrderInfo_AllAction',
        success: function (data) {
          _this.tableData = data;
          _this.total = _this.tableData.length
          console.log(data);
        },
        complete: function (XMLHttpRequest) {
          _this.loading = false
          if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
            window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
          }
        },
        dataType: "json"
      });
    },
    getList() {     //换页方法
      let _this = this
      $.ajax({
        type: 'POST',
        //	url: "../getContractListByConditionAction",
        url: "../getListByTableNameAction",
        data: {
          Data: "enterprise_info"
        },
        complete: function (XMLHttpRequest) {
          _this.loading = false
          if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
            window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
          }
        },
        success: function (Result) {
          console.log(Result);
          _this.tableData = Result      //将获取的数据存到表格数据中
        },

        dataType: "json"
      });
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
      console.log(this.multipleSelection);
    },
    beforeRemove(file, fileList) {
      this.fileList.pop()
    },
    uploadDetails(file) {
      this.fileList.push({
        name: file.name,
        url: file.type
      })
      let _this = this
      var reader = new FileReader();
      reader.readAsBinaryString(file);
      reader.onload = function (e) {
        var data = e.target.result;
        var wb = XLSX.read(data, {
          type: 'binary' //以二进制的方式读取
        });
        var sheet0 = wb.Sheets[wb.SheetNames[0]];//sheet0代表excel表格中的第一页
        var str = XLSX.utils.sheet_to_json(sheet0);//利用接口实现转换。
        console.log(str);
        //将整体数据分解成每一条，一条上传一次

        //1.将接受到数组便利
        //2.将栏目存到_this.sales_unitprice里并去重
        str.forEach(item => {
          _this.form2.push({
            Order_SerialNumber: '',
            Sales_Order_Detail_SerialNumber: item['销售订单明细流水号'],
            Store_SerialNumber: item['门店号'],
            Article_SerialNumber: item['商品'],
            Quantity: item['数量'],
            Supplier_SerialNumber: item['可选择供应商']
          })
        })
      }
    },
  },
})